<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #list {
        cursor: pointer;
      }
      li {
        border: solid 1px;
        margin: 5px;
      }
      .selected {
        color: red;
        background-color: antiquewhite;
      }
    </style>
  </head>
  <body>
    <ul id="list">
      <li>li1</li>
      <li class="selected">li2</li>
      <li>li3</li>
      <li>li4</li>
    </ul>
    <script>
      const ul = document.getElementById("list");
      ul.onclick = function (e) {
        const target = e.target;
        if (target.tagName === "LI") {
          // 获取已经选择的
          if (e.ctrlKey === false) {
            ul.querySelectorAll(".selected").forEach((el) =>
              el.classList.remove("selected")
            );
          }
          target.classList.add("selected");
        }
      };
    </script>
  </body>
</html>
